using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * Autor      : IT-Developer1    
 * Data       : 02/04/2010 11:03:48
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CFamiliasBeneficiarios
   {
       private string IDT_BENEF ;
       private string IDT_BENEF_PARENTE ;
       private string NOM_BENEF_PARENTE;
       private string IDT_GRAU ;

	   /// <sumary>
       /// Construtor da classe familias_beneficiarios
       /// <sumary>
       public CFamiliasBeneficiarios()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Familias_beneficiarios
       /// <sumary>
       public CFamiliasBeneficiarios( string IDT_BENEF, string IDT_BENEF_PARENTE, string IDT_GRAU )
       {
          this.IDT_BENEF = IDT_BENEF;
          this.IDT_BENEF_PARENTE = IDT_BENEF_PARENTE;
          this.IDT_GRAU = IDT_GRAU;
       }

       /// <sumary>
       /// Construtor da classe Familias_beneficiarios
       /// <sumary>
       public CFamiliasBeneficiarios(string IDT_BENEF, string IDT_BENEF_PARENTE, string NOM_BENEF, string IDT_GRAU)
       {
           this.IDT_BENEF = IDT_BENEF;
           this.IDT_BENEF_PARENTE = IDT_BENEF_PARENTE;
           this.NOM_BENEF_PARENTE = NOM_BENEF;
           this.IDT_GRAU = IDT_GRAU;
       }

	   #region Propriedades
     	/// <sumary>
        /// 
        /// <sumary>
        public string Idt_benef 
		{
		   get
		   {
		       return this.IDT_BENEF;
		   }
		   set
		   {
		       this.IDT_BENEF = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Idt_benef_parente 
		{
		   get
		   {
		       return this.IDT_BENEF_PARENTE;
		   }
		   set
		   {
		       this.IDT_BENEF_PARENTE = value;
		   }
		}
        public string Nom_benef_parente
        {
            get
            {
                return this.NOM_BENEF_PARENTE;
            }
            set
            {
                this.NOM_BENEF_PARENTE = value;
            }
        }
        /// <sumary>
        /// Grau de parentesco
        /// <sumary>
        public string Idt_grau 
		{
		   get
		   {
		       return this.IDT_GRAU;
		   }
		   set
		   {
		       this.IDT_GRAU = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Familias_beneficiarios.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_FAMILIAS_BENEFICIARIOS
			                  ( Idt_Benef, IDT_BENEF_PARENTE, IDT_GRAU )
						   values
						      ( :Idt_Benef, :IDT_BENEF_PARENTE, :IDT_GRAU )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("Idt_Benef",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_BENEF_PARENTE",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_GRAU",OracleType.Number)); 
				
                command.Parameters["Idt_Benef"].Value = this.IDT_BENEF;
                command.Parameters["IDT_BENEF_PARENTE"].Value = this.IDT_BENEF_PARENTE;
                command.Parameters["IDT_GRAU"].Value = this.IDT_GRAU;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Familias_beneficiarios.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_FAMILIAS_BENEFICIARIOS set 
			                   IDT_GRAU = :IDT_GRAU
						   where
						       Idt_Benef = :Idt_Benef AND IDT_BENEF_PARENTE = :IDT_BENEF_PARENTE ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("Idt_Benef",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_BENEF_PARENTE",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_GRAU",OracleType.Number)); 
				
                command.Parameters["Idt_Benef"].Value = this.IDT_BENEF;
                command.Parameters["IDT_BENEF_PARENTE"].Value = this.IDT_BENEF_PARENTE;
                command.Parameters["IDT_GRAU"].Value = this.IDT_GRAU;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Familias_beneficiarios.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_FAMILIAS_BENEFICIARIOS", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("Idt_Benef", DbType.Int32, this.IDT_BENEF));
            tab.adicionarFiltro(new Coluna("IDT_BENEF_PARENTE", DbType.Int32, this.IDT_BENEF_PARENTE));
            tab.delete();
        }
		
		static public DataTable ObterTodos()
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_FAMILIAS_BENEFICIARIOS order by Idt_Benef ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CFamiliasBeneficiarios PesquisaporCodigo(string idt_benef, string idt_benef_parente, string login)
        {
            CFamiliasBeneficiarios obj;
            string SQL = @"SELECT FB.Idt_Benef, FB.IDT_BENEF_PARENTE, B.NOM_BENEF, FB.IDT_GRAU
                            FROM Serix.SRX_FAMILIAS_BENEFICIARIOS FB JOIN Serix.SRX_BENEFICIARIOS B ON (FB.IDT_BENEF_PARENTE = B.Idt_Benef) 
                            WHERE FB.Idt_Benef = :Idt_Benef AND FB.IDT_BENEF_PARENTE = :IDT_BENEF_PARENTE";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":Idt_Benef", DbType.String, idt_benef);
				qy.AdicionarParametro(":IDT_BENEF_PARENTE", DbType.String, idt_benef_parente);
               
				qy.Executar();
                obj = new CFamiliasBeneficiarios(qy.ObterCampo("Idt_Benef"), qy.ObterCampo("IDT_BENEF_PARENTE"), qy.ObterCampo("NOM_BENEF"), qy.ObterCampo("IDT_GRAU"));
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CFamiliasBeneficiarios();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_BENEFIDT_BENEF_PARENTE() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
		
		static public DataTable ObterPorIDT_BENEF(string idt_benef)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_FAMILIAS_BENEFICIARIOS  where Idt_Benef = :Idt_Benef order by Idt_Benef ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("Idt_Benef", OracleType.Number));
                command.Parameters["Idt_Benef"].Value = idt_benef;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorIDT_BENEF_PARENTE(string idt_benef_parente)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_FAMILIAS_BENEFICIARIOS  where IDT_BENEF_PARENTE = :IDT_BENEF_PARENTE order by Idt_Benef ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_BENEF_PARENTE", OracleType.Number));
                command.Parameters["IDT_BENEF_PARENTE"].Value = idt_benef_parente;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorIDT_GRAU(string idt_grau)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_FAMILIAS_BENEFICIARIOS  where IDT_GRAU = :IDT_GRAU order by Idt_Benef ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_GRAU", OracleType.Number));
                command.Parameters["IDT_GRAU"].Value = idt_grau;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
    }
}	

